﻿

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Playkanaler.WebPageSource;
using Playkanaler.WebPageSource.HtmlParser;

namespace Playkanaler.Channels.Tv4
{

    public class TV4StartSetting : SettingFromWeb
    {
        public TV4StartSetting()
        {
            Page = new TV4Page();
            View = new ListView();
        }
        public override void ClickItem(Item item)
        {
           
            TV4Setting ny = new TV4Setting();
            ny.Page = new TV4Page() { Url = item.Path };
            ny.Heading = item.Title;
            ny.Navigate();
        }
    }


    public class TV4Setting : SettingFromWeb
    {
        public TV4Setting()
        {
            Page = new TV4Page();            
            View = new EpView();
        }
        public override void ClickItem(Item item)
        {
            //play
        }
    }

    public class TV4Page : BasicPage
    {

        public TV4Page()
        {
            Url = "http://www.tv4play.se";           
        }
        HtmlDoc doc;
        protected override bool LoadPage()
        {



            WebClient wc = new WebClient();
            wc.Encoding = UTF8Encoding.UTF8;
            wc.Headers.Add("User-Agent", "Mozilla/4.0+");

            string dlstr = wc.DownloadString(Url);

            doc = new HtmlDoc();
            doc.Parse(dlstr);
            doc.CreateTree();

            

            
            if (Url != "http://www.tv4play.se")
            {
                parseClips();                           

            }
            else
            {
                parsePrograms();               

            }

            Tabs.Reverse();
            Items.Reverse();


            return true;

        }


        void parsePrograms()
        {
            Tabs.Add(new PageLink("Namn") { selected = true });


            var uls = doc.Root
                .Child("body")
                 .Child("div", "id", "wrapper")
                  .Child("div", "id", "droppables")
                   .Child("div", "id", "contents-container")
                    .Child("div")
                     .Children("ul");

            foreach (var ul in uls)
            {
                foreach (var li in ul.Children("li"))
                {
                    foreach (var a in li.Children("a", "class", "play"))
                    {
                        PageLink item = new PageLink();
                        item.name = a.text;
                        item.url = a.attribute["href"];
                        Items.Add(item);
                    }

                }
            }
        }
        void parseClips()
        {
            Tag divmeat = null;


            //find <div class=meat

            var divrow = doc.Root
               .Child("body")
                .Child("div", "id", "wrapper")
                 .Children("div", "class", "row");

            foreach (var row in divrow)
            {
                if (row.HasChild("div", "id", "browser"))
                {
                    divmeat = row
                      .Child("div", "id", "browser")
                       .Child("div", "class", "meat");
                }
            }


            // parse tabs

            var tabsul = divmeat
            .Child("div", "class", "tab-section")
             .Child("ul");

            foreach (var li in tabsul.Children())
            {
                PageLink pl = new PageLink();
                pl.selected = (li.attribute["class"] == "button selected");
                var a = li.Child("h3").Child("a");
                pl.name = a.text;
                pl.url = FixUrl(a.attribute["href"]);
                Tabs.Add(pl);
            }


            // parse content
            var uls = divmeat
                .Child("div", "class", "wrapper")
                 .Child("div", "class", "content")
                  .Children("ul");

            foreach (var ul in uls)
            {
                foreach (var li in ul.Children())
                {
                    var div = li.Child("div");
                    PageLink pl = new PageLink();
                    pl.imageurl = FixUrl(div.Child("a").Child("img").attribute["src"]);
                    var a = div.Child("p", null).Child("a");
                    pl.name = a.text;
                    pl.url = FixUrl(a.attribute["href"]);
                    Items.Add(pl);
                }
            }


            if (divmeat.Child("div", "class", "pagination").HasChild("ul"))
            {
                var an = divmeat.Child("div", "class", "pagination").Child("ul").Children("li", "class", "button number").First().Child("a");
                NumContentPages = int.Parse(an.text);
                ContentPageNavTemplate = an.attribute["href"];
                ContentPageNavTemplate = ContentPageNavTemplate.Remove(ContentPageNavTemplate.LastIndexOf("=") + 1) + "{PageNumber}";
                ContentPageNavTemplate = ContentPageNavTemplate.Replace("&amp;", "&");
                ContentPageNavTemplateReplaceStr = "{PageNumber}";

                Uri uri = new Uri(Url);
                ContentPageNavTemplate = "http://" + uri.Host + uri.LocalPath + ContentPageNavTemplate;

            }
            else NumContentPages = 0;
        }
    }
}